package com.itheima.nio.tomcat.process;

import com.itheima.nio.tomcat.domain.HttpRequest;
import com.itheima.nio.tomcat.domain.HttpResponse;
import com.itheima.nio.tomcat.inter.HttpServlet;
import com.itheima.nio.tomcat.inter.ServletConcurrentHashMap;
import com.itheima.nio.tomcat.servlet.AServlet;
import com.itheima.nio.tomcat.servlet.BServet;
import com.itheima.nio.tomcat.servlet.UserServlet;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;

/**
 * 动态资源的处理类
 */
public class DynamicResourceProcess {

    // 处理用户以/servlet开始的请求路径
    public void process(HttpRequest httpRequest , HttpResponse httpResponse) {

        // 根据请求的requestURI从concurrentHashMap，获取Servlet对象，然后调用service方法进行执行
        String requestURI = httpRequest.getRequestURI();
        HttpServlet httpServlet = ServletConcurrentHashMap.concurrentHashMap.get(requestURI);   // 是多态的体现

        // 对获取到的HttpServlet对象进行判空操作
        if(httpServlet != null) {
            httpServlet.service(httpRequest , httpResponse);
        }else {
            response404(httpResponse);
        }

        // 根据不同的请求路径我们去访问不同的servlet
//        String requestURI = httpRequest.getRequestURI();
//        if(requestURI.equals("/servlet/AServlet")) {        // 去执行AServlet中的service方法，处理本次请求
//            AServlet servlet = new AServlet() ;
//            servlet.service(httpRequest , httpResponse);
//        }else if(requestURI.equals("/servlet/UserServlet")) {   // 去执行UserServlet中的service方法，处理本次请求
//
//            // 创建UserServlet对象，调用它的service
//            UserServlet userServlet = new UserServlet();
//            userServlet.service(httpRequest , httpResponse);
//
//        } else if(requestURI.equals("/servlet/BServlet")) {   // 去执行BServlet中的service方法，处理本次请求
//
//            BServet bServet = new BServet();
//            bServet.service(httpRequest , httpResponse);
//        }

        // 给出响应
        // httpResponse.setContentType("text/html;charset=UTF-8");
        // httpResponse.write("OK,UserServlet执行了..........");

    }

    // 给客户端404响应
    private void response404(HttpResponse httpResponse) {

        // 构造响应行，响应头，响应空行
        String responseLine = "HTTP/1.1 404 NOT FOUND\r\n" ;
        String responseHeader = "Content-Type: text/html;charset=UTF-8\r\n" ;
        String emptyLine = "\r\n" ;
        String result = responseLine + responseHeader + emptyLine ;

        try {

            // 把响应行，响应头，响应空行写给客户端
            SelectionKey selectionKey = httpResponse.getSelectionKey();
            SocketChannel socketChannel = (SocketChannel) selectionKey.channel() ;
            ByteBuffer byteBuffer = ByteBuffer.wrap(result.getBytes("UTF-8"));
            socketChannel.write(byteBuffer) ;

            // 写响应体数据
            socketChannel.write(ByteBuffer.wrap("404 NOT FOUND...........".getBytes("UTF-8"))) ;
            socketChannel.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
